﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Security.Cryptography;

using AddressBook.Entity;

/// <summary>
///Common 的摘要说明
/// </summary>
public class Common
{
    public Common()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    public static string ReturnResult(string name, string contents, string resultCode, string desc)
    {
        StringBuilder result = new StringBuilder();
        result.Append("{");
        result.AppendFormat("\"Result\":\"{2}\",\"Desc\":\"{3}\",\"UpdateTime\":\"{0}\",\"Data\":[", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), name, resultCode, desc);
        result.Append(contents);
        result.Append("]}");
        return result.ToString();
    }

    public static string ReturnResult(ResultEntity entity)
    {
        return ReturnResult(entity.Name, entity.Contents, entity.ResultCode, entity.Desc);
    }

    /// <summary>
    /// MD5函数(16位)
    /// </summary>
    /// <param name="str">原始字符串</param>
    /// <returns>MD5结果</returns>
    public static string StringToMD516(string str)
    {
        //16位MD5加密（取32位加密的9~25字符）
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
    }

    /// <summary>
    /// MD5函数(32位)
    /// </summary>
    /// <param name="str">原始字符串</param>
    /// <returns>MD5结果</returns>
    public static string StringToMD532(string str)
    {
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
    }
}
